package com.amqr.movetest;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class CardChildAllNumAdapter extends RecyclerView.Adapter<CardChildAllNumAdapter.ViewHolder> {
    public List<String> mDatas = null;
    public Context mContext;

    // 点击回调
    public interface OnItemClickLitener {
        void onItemClick(View view, int position);  // 点击
        void onItemLongClick(View view, int position); // 长按
    }

    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    public CardChildAllNumAdapter(Context context, List<String> datas) {
        this.mDatas = datas;
        this.mContext = context;
    }


    public void removeItem(String datas) {
        this.mDatas.remove(datas);
    }


    public void setDatas(ArrayList<String> datas) {
        this.mDatas = datas;
    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_rv, viewGroup, false);
        return new ViewHolder(view);
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, final int position) {

        // 如果设置了回调，则设置点击事件

        String str = mDatas.get(position);
        viewHolder.mTvPhoneChildNum.setText(str);
        viewHolder.mAllCallNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mOnItemClickLitener!=null){
                    mOnItemClickLitener.onItemClick(viewHolder.mAllCallNum,position);
                }
            }
        });

        viewHolder.mAllCallNum.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if(mOnItemClickLitener!=null){
                    mOnItemClickLitener.onItemLongClick(viewHolder.mAllCallNum,position);
                }
                return true;
            }
        });

        // 本来的监听不可不必，但是可能嵌套的原因导致单击文本不生效，所以才有了下面的鹅这两个方法
        viewHolder.mTvPhoneChildNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mOnItemClickLitener!=null){
                    mOnItemClickLitener.onItemClick(viewHolder.mAllCallNum,position);
                }
            }
        });

        viewHolder.mTvPhoneChildNum.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if(mOnItemClickLitener!=null){
                    mOnItemClickLitener.onItemLongClick(viewHolder.mAllCallNum,position);
                }
                return true;
            }
        });
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout mLLCallNum;
        private LinearLayout mAllCallNum;
        public TextView mTvPhoneChildNum;

        public ViewHolder(View view) {
            super(view);
            mLLCallNum = (LinearLayout) view.findViewById(R.id.mLLCallNum);
            mAllCallNum = (LinearLayout) view.findViewById(R.id.mAllCallNum);
            mTvPhoneChildNum = (TextView) view.findViewById(R.id.mTvPhoneChildNum);

        }
    }
}
